package cc.iotkit.data.service.impl;

import cc.iotkit.common.api.Paging;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.mapper.ProtocolComponentMapper;
import cc.iotkit.data.model.TbProtocolComponent;
import cc.iotkit.data.service.ProtocolComponentService;
import cc.iotkit.model.protocol.ProtocolComponent;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 石恒
 * @Date: 2023/7/26 22:50
 * @Description:
 */
@Service
@Primary
public class ProtocolComponentServiceImpl extends ServiceImpl<ProtocolComponentMapper, TbProtocolComponent> implements ProtocolComponentService {

    @Override
    public List<ProtocolComponent> findByState(String state) {
        List<TbProtocolComponent> protocolComponentList = this.baseMapper.selectList(new LambdaQueryWrapper<TbProtocolComponent>()
                .eq(StringUtils.isNotBlank(state), TbProtocolComponent::getState, state));
        return MapstructUtils.convert(protocolComponentList, ProtocolComponent.class);
    }

    @Override
    public List<ProtocolComponent> findByStateAndType(String state, String type) {
        List<TbProtocolComponent> protocolComponentList = this.baseMapper.selectList(new LambdaQueryWrapper<TbProtocolComponent>()
                .eq(StringUtils.isNotBlank(state), TbProtocolComponent::getState, state)
                .eq(StringUtils.isNotBlank(type), TbProtocolComponent::getType, type));
        return MapstructUtils.convert(protocolComponentList, ProtocolComponent.class);
    }

    @Override
    public List<ProtocolComponent> findByUid(String uid) {
        List<TbProtocolComponent> protocolComponentList = this.baseMapper.selectList(new LambdaQueryWrapper<TbProtocolComponent>()
                .eq(StringUtils.isNotBlank(uid), TbProtocolComponent::getUid, uid));
        return MapstructUtils.convert(protocolComponentList, ProtocolComponent.class);
    }

    @Override
    public Paging<ProtocolComponent> findByUid(String uid, int page, int size) {
        Page<TbProtocolComponent> rowPage = new Page<>(page, size);
        Page<TbProtocolComponent> deviceGroupPage = this.baseMapper.selectPage(rowPage, new LambdaQueryWrapper<TbProtocolComponent>().eq(TbProtocolComponent::getUid, uid));
        Paging<ProtocolComponent> paging = new Paging<>();
        paging.setRows(MapstructUtils.convert(deviceGroupPage.getRecords(), ProtocolComponent.class));
        paging.setTotal(rowPage.getTotal());
        return paging;
    }

    @Override
    public Page<TbProtocolComponent> findAll(int page, int size) {
        Page<TbProtocolComponent> rowPage = new Page<>(page, size);
        return this.baseMapper.selectPage(rowPage, new LambdaQueryWrapper<>());
    }

    @Override
    public long countByUid(String uid) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<TbProtocolComponent>()
                .eq(StringUtils.isNotBlank(uid), TbProtocolComponent::getUid, uid));
    }
}
